﻿using Kingdee.BOS.Orm.DataEntity;
using SD.WRZS.K3.ServiceFacade.KDServiceEntity.Mobile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDWXServer.Models
{
    /// <summary>
    /// 采购订单
    /// </summary>
    [Serializable]
    public class WXPurchaseOrder : AbstractBillObject
    {
        public WXPurchaseOrder()
        {
            Organization = new Organization();
            Material = new Material();
            DocumentStatusName = "";
            DeliveryDate = DateTime.Now.Date;
            Supplier = new Supplier();
            PCJH = new WXPCJHItem();
        }
        /// <summary>
        /// 单据状态名称
        /// </summary>
        public string DocumentStatusName { get; set; }
        /// <summary>
        /// 交货地点
        /// </summary>
        public Organization Organization { get; set; }
        /// <summary>
        /// 物料
        /// </summary>
        public Material Material { get; set; }
        /// <summary>
        /// 计划数量
        /// </summary>
        public decimal PlanQty { get; set; }
        /// <summary>
        /// 入库数量
        /// </summary>
        public decimal StockQty { get; set; }

        /// <summary>
        /// 交货日期
        /// </summary>
        public DateTime DeliveryDate { get; set; }
        /// <summary>
        /// 供应商
        /// </summary>
        public Supplier Supplier { get; set; }
        /// <summary>
        /// 派车计划
        /// </summary>
        public WXPCJHItem PCJH { get; set; }
        public static WXPurchaseOrder Parse(DynamicObject obj)
        {
            var item = new WXPurchaseOrder();
            try
            {
                item.Id = Convert.ToInt64(obj["Id"]);
                item.BillNo = Convert.ToString(obj["BillNo"]);
                item.BillDate = Convert.ToDateTime(obj["Date"]);
                item.BillTypeId = Convert.ToString(obj["BillTypeId_Id"]);
                item.BillTypeName = Convert.ToString((obj["BillTypeId"] as DynamicObject)["Name"]);
                item.DocumentStatus = Convert.ToString(obj["DocumentStatus"]);
                item.CloseStatus = Convert.ToString(obj["CloseStatus"]);
                switch (item.DocumentStatus)
                {
                    case "Z":
                        item.DocumentStatusName = "暂存";
                        break;
                    case "A":
                        item.DocumentStatusName = "创建";
                        break;
                    case "B":
                        item.DocumentStatusName = "审核中";
                        break;
                    case "C":
                        item.DocumentStatusName = "已审核";
                        break;
                    case "D":
                        item.DocumentStatusName = "重新审核";
                        break;
                }
                if (item.CloseStatus == "B")
                {
                    item.DocumentStatusName = "已关闭";
                }

                var orgObj = obj["PurchaseOrgId"] as DynamicObject;
                item.Organization.Id = Convert.ToInt64(orgObj["Id"]);
                item.Organization.Number = Convert.ToString(orgObj["Number"]);
                item.Organization.Name = Convert.ToString(orgObj["Name"]);

                var supplier = obj["SupplierId"] as DynamicObject;
                item.Supplier.Id = Convert.ToInt64(supplier["Id"]);
                item.Supplier.MasterId = Convert.ToInt64(supplier["msterID"]);
                item.Supplier.Number = Convert.ToString(supplier["Number"]);
                item.Supplier.Name = Convert.ToString(supplier["Name"]);

                var entryRows = obj["POOrderEntry"] as DynamicObjectCollection;
                var row = entryRows[0];
                var material = row["MaterialId"] as DynamicObject;
                item.Material.Id = Convert.ToInt64(material["Id"]);
                item.Material.MasterId = Convert.ToInt64(material["msterID"]);
                item.Material.Number = Convert.ToString(material["Number"]);
                item.Material.Name = Convert.ToString(material["Name"]);
                item.Material.Specification = Convert.ToString(material["Specification"]);

                var unit = row["UnitId"] as DynamicObject;
                item.Material.StockUnit.Id = Convert.ToInt64(unit["Id"]);
                item.Material.StockUnit.Number = Convert.ToString(unit["Number"]);
                item.Material.StockUnit.Name = Convert.ToString(unit["Name"]);

                item.Material.SaleUnit.Id = item.Material.StockUnit.Id;
                item.Material.SaleUnit.Number = item.Material.StockUnit.Number;
                item.Material.SaleUnit.Name = item.Material.StockUnit.Name;

                item.Material.PurchaseUnit.Id = item.Material.StockUnit.Id;
                item.Material.PurchaseUnit.Number = item.Material.StockUnit.Number;
                item.Material.PurchaseUnit.Name = item.Material.StockUnit.Name;

                item.PlanQty = Convert.ToDecimal(row["Qty"]);
                item.StockQty = Convert.ToDecimal(row["StockInQty"]);
                item.DeliveryDate = Convert.ToDateTime(row["DeliveryDate"]);

                var subRows = row["FTruckPlanEntity_SD"] as DynamicObjectCollection;
                if (subRows.Count > 0)
                {
                    var subRow = subRows[0];
                    item.PCJH.Id = Convert.ToInt64(subRow["Id"]);
                    item.PCJH.PlateNumber = Convert.ToString(subRow["FCarNum"]);
                    item.PCJH.Driver = Convert.ToString(subRow["FDriver"]);
                    item.PCJH.IDCardNo = Convert.ToString(subRow["FIDCardNO"]);
                    item.PCJH.Mobile = Convert.ToString(subRow["FMobile"]);
                    item.PCJH.Qty = Convert.ToDecimal(subRow["FPCQty"]);

                    item.PCJH.YunFeiPrice = Convert.ToDecimal(subRow["FYunFeiPrice"]);
                    item.PCJH.TruckTypeId = Convert.ToString(subRow["FTruckTypeId_Id"]);
                    if (subRow["FTruckTypeId"] != null)
                    {
                        item.PCJH.TruckTypeName = Convert.ToString((subRow["FTruckTypeId"] as DynamicObject)["FDataValue"]);
                    }
                    item.PCJH.ForbidStatus = Convert.ToString(subRow["FRowForbidStatus"]);
                    item.PCJH.Note = Convert.ToString(subRow["FPCJHNote_SD"]);
                    item.PCJH.ExpiryTime = Convert.ToDateTime(subRow["FPCJHExpiryTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                    //item.PCJH.RoadTransportCN = Convert.ToString(subRow["FDLYSZH"]);
                    //item.PCJH.EmploymentQualificationCN = Convert.ToString(subRow["FCYZGZH"]);
                    //var cheZhou = subRow["FCheZhou"] as DynamicObject;
                    //if (cheZhou != null)
                    //{
                    //    item.PCJH.CheZhou.Id = Convert.ToInt64(cheZhou["Id"]);
                    //    item.PCJH.CheZhou.Number = Convert.ToString(cheZhou["Number"]);
                    //    item.PCJH.CheZhou.Name = Convert.ToString(cheZhou["Name"]);
                    //}
                }
            }
            catch (Exception e)
            {
                KD.Logger.Error("WXPurchaseOrder", e);
            }

            return item;
        }
    }
    /// <summary>
    /// 供应商
    /// </summary>
    [Serializable]
    public class WXSupplier : Supplier
    {
        public WXSupplier()
        {
            OpenBankName = "";
            BankHolder = "";
            BankCode = "";
            Contact = "";
            Plate = "";
            Organization = new Organization();
        }
        /// <summary>
        /// 账户名称
        /// </summary>
        public string BankHolder { get; set; }
        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact { get; set; }
        /// <summary>
        /// 交货地点
        /// </summary>
        public Organization Organization { get; set; }
        /// <summary>
        /// 车牌号
        /// </summary>
        public string Plate { get; set; }
        public static WXSupplier Parse(DynamicObject obj)
        {
            var item = new WXSupplier();
            item.Id = Convert.ToInt64(obj["Id"]);
            item.MasterId = Convert.ToInt64(obj["msterID"]);
            item.Number = Convert.ToString(obj["Number"]);
            item.Name = Convert.ToString(obj["Name"]);

            var org = obj["CreateOrgId"] as DynamicObject;
            item.Organization.Id = Convert.ToInt64(org["Id"]);
            item.Organization.Number = Convert.ToString(org["Number"]);
            item.Organization.Name = Convert.ToString(org["Name"]);

            var lxrEntry = obj["SupplierContact"] as DynamicObjectCollection;
            if (lxrEntry.Count > 0)
            {
                var lxrRow = lxrEntry.Where(p => Convert.ToBoolean(p["IsDefault"])).FirstOrDefault();
                if (lxrRow == null) lxrRow = lxrEntry[0];

                item.Mobile = Convert.ToString(lxrRow["Mobile"]);
                item.IDCardNo = Convert.ToString(lxrRow["Tel"]);
            }
            var bankEntry = obj["SupplierBank"] as DynamicObjectCollection;
            if (bankEntry.Count > 0)
            {
                var bankRow = bankEntry.Where(p => Convert.ToBoolean(p["IsDefault"])).FirstOrDefault();
                if (bankRow == null) bankRow = bankEntry[0];

                item.OpenBankName = Convert.ToString(bankRow["OpenBankName"]);
                item.BankCode = Convert.ToString(bankRow["BankCode"]);
                item.BankHolder = Convert.ToString(bankRow["BankHolder"]);
            }

            return item;
        }
    }
    /// <summary>
    /// 客户
    /// </summary>
    [Serializable]
    public class WXCustomer : Customer
    {
        public static WXCustomer Parse(DynamicObject obj)
        {
            var cust = new WXCustomer();
            cust.Id = Convert.ToInt64(obj["Id"]);
            cust.MasterId = Convert.ToInt64(obj["msterID"]);
            cust.Number = Convert.ToString(obj["Number"]);
            cust.Name = Convert.ToString(obj["Name"]);

            return cust;
        }
    }
}